Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve support for enum_exists #7404

Merged
merged 1 commit into from
Jan 22, 2022
Merged

improve support for enum_exists #7404

merged 1 commit into from
Jan 22, 2022

Conversation

orklah
Copy link
Collaborator

@orklah orklah commented Jan 16, 2022

This introduces:

  • enum-string as an assertion
  • the possibility to use enum-string as a type in phpdoc
  • autoloading of enum-string used in enum_exists
  • some documentation
  • some fixes in callmaps and stubs

This is built upon the work of @DannyvdSluijs started here: #6431 (comment)

This should fix #6431 but I'm pretty sure there are still some support missing, it will have to be reported in the future so we can know there are issues

@orklah orklah added the release:feature The PR will be included in 'Features' section of the release notes label Jan 16, 2022
// We're using class_exists here because enum_exists doesn't exist on old versions of PHP
// Not sure what happens if we try to autoload or reflect on an enum on an old version of PHP though...
if ($string_value && class_exists($string_value)) {
$reflection_class = new ReflectionClass($string_value);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given a ReflectionClass in constructed anyway why not check (in an php <= 8.1 safe way) if it is an actual enum using:

array_search ('UnitEnum', $c->getInterfaceNames(), true);

As per https://www.php.net/manual/en/class.unitenum.php all Enums implement the UnitEnum interface

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it will only work if PHP was able to autoload the file and parse it and detect it is indeed an Enum. Which mean it will only work on PHP >8.1. At this point, we might as well use enum_exists as long as we checked we're in 8.1.

Thinking about it however, I don't see a point preventing scanning of a possible class that would have been pushed in an enum_exists. Either it exists and it's useful and it will end up scanned at one point or it's useless and it's possible Psalm will actually report it unused

Anyway, the real issue is for older versions of PHP, I have no idea what will happen if we run Psalm on 8.0 and try to analyze an enum. It will probably crash? But I don't think I can prevent it

@orklah
Copy link
Collaborator Author

orklah commented Jan 19, 2022

I'll put this in draft. I'll probably start this again from scratch on master to avoid conflicts with #7410

@orklah orklah marked this pull request as draft January 19, 2022 19:34
@DannyvdSluijs
Copy link

Hi @orklah anything I can to to assist either with this PR (#7404) or with #7410 ?
I can actually run your branch against some 8.1 code using enum on a PHP 8.0 binary to see if this would cause any issues? If there is anything else please let me know as I'm keen on getting the enum_exists feature in.

@orklah
Copy link
Collaborator Author

orklah commented Jan 20, 2022

#7410 is a pretty complex refactoring of a pretty complex module of Psalm. That's why I don't want to get in the way because I can't wait to see it happen :)

This PR in comparison is simple and I'm not worried I'll be able to do it again quickly once #4710 is merged. You should be able to test Psalm with this PR if you want, it should work OK and if you detect bugs, I can fix them.

This will be added to Psalm 5 that should came out in a few weeks

@orklah
Copy link
Collaborator Author

orklah commented Jan 20, 2022

This is not blocked anymore, I'll work on it tomorrow :)

@orklah orklah changed the base branch from 4.x to master January 22, 2022 17:09
@orklah orklah marked this pull request as ready for review January 22, 2022 17:27
@orklah orklah merged commit 4ed8355 into vimeo:master Jan 22, 2022
@orklah
Copy link
Collaborator Author

orklah commented Jan 22, 2022

@DannyvdSluijs it should be ok on master now

@DannyvdSluijs
Copy link

I've checked it and it seems to be working very nicely, thanks for your efforts.

To summarise the results:
Running ./vendor/bin/psalm --php-version=8.1 --no-cache with PHP 7.4 results in a No errors found!
Leaving off the --php-version argument will target PHP 7.4 and returns the following errors (as part of the regular output.)

Target PHP version: 7.4 (inferred from current PHP version)
Scanning files...
Analyzing files...

EE

ERROR: ParseError - src/SomeEnum.php:8:6 - Syntax error, unexpected T_STRING on line 8 (see https://psalm.dev/173)
enum SomeEnum


ERROR: UndefinedFunction - src/Test.php:13:13 - Function enum_exists does not exist (see https://psalm.dev/021)
        if (\enum_exists(SomeEnum::class)) {


ERROR: UndefinedClass - src/Test.php:13:26 - Class, interface or enum named Dannyvandersluijs\Test\SomeEnum does not exist (see https://psalm.dev/019)
        if (\enum_exists(SomeEnum::class)) {


ERROR: UndefinedClass - src/Test.php:14:13 - Class, interface or enum named Dannyvandersluijs\Test\SomeEnum does not exist (see https://psalm.dev/019)
            SomeEnum::cases();


------------------------------
4 errors found
------------------------------

Checks took 2.35 seconds and used 42.146MB of memory
Psalm was unable to infer types in the codebase

@orklah
Copy link
Collaborator Author

orklah commented Jan 25, 2022

the ParserError comes from our dependency (PHP-Parser) when it encounters a syntax it doesn't recognize for a given php version.

The rest of the errors is the effect of the first because Psalm couldn't see there is an enum here.

Those errors are indeed expected to happen on 7.4 :)

Great to know it works for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Needs work release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enums: new function enum_exists
2 participants